import { BlockEnum, CommonNodeType, NodeDefault } from '@/components/workflow/types'
import { generateUUID } from '@/utils'
import { ALL_COMPLETION_AVAILABLE_BLOCKS } from '../../constants'
import { InputVar, OutputVar } from '../types'

export type KnowledgeListType = {
  id: number
  name: string
  icon?: string
  description?: string
}

export type KnowledgeNodeType = CommonNodeType & {
  inputList: InputVar[]
  knowledgeList: KnowledgeListType[]
  outputList: OutputVar[]
}
export const getKnowledgeDefaultParams = (): any => {
  return {
    inputList: [getKnowledgeInputListParams()],
    knowledgeList: [],
    // 输出
    outputList: [
      {
        variable: 'output',
        value: '知识库输出',
        type: 'string',
        value_selector: []
      }
    ]
  }
}

export const getKnowledgeInputListParams = (): InputVar => {
  return {
    id: generateUUID(8),
    type: 'cite',
    label: '',
    variable: 'query',
    value: '',
    max_length: 10,
    default: '',
    required: true,
    options: [],
    value_selector: []
  }
}

const nodeDefault: NodeDefault<KnowledgeNodeType> = {
  defaultValue: { ...getKnowledgeDefaultParams() },
  getAvailablePrevNodes() {
    const nodes = ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
    return nodes
  },
  getAvailableNextNodes() {
    const nodes = ALL_COMPLETION_AVAILABLE_BLOCKS
    return nodes
  },
  checkValid(payload: KnowledgeNodeType) {
    let isValid = true
    let errorMessages = ''
    if (payload.type) {
      isValid = true
      errorMessages = ''
    }
    return {
      isValid,
      errorMessage: errorMessages
    }
  }
}

export default nodeDefault
